<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_string_helper
 *
 * @author Simon Emms <simon@simonemms.com>
 */



/**
 * Is Serialized
 *
 * Checks if a string is a serialized array. There's
 * no way of *GUARANTEEING* that it's a serialized
 * array, but checks for most signs that it is
 *
 * @param string $string
 * @return boolean
 */
function is_serialized($string = null) {
    if(!is_null($string)) {
        return (bool) preg_match('/^(a|O|s|b|i):/', $string);
    }
    return false;
}








/**
 * JSON Encode
 *
 * Encodes an array as a JSON string.  JSON_Encode
 * is a native PHP function in PHP 5.2 onwards.
 *
 * @author Simon Emms <simon@simonemms.com>
 */
if(!function_exists('json_encode')) {
    function json_encode($array = false) {
        if(is_null($array)) { return 'null'; }
        if($array === false) { return 'false'; }
        if($array === true) { return 'true'; }
        
        if(is_scalar($array)) {
            if(is_float($array)) {
                /* Always use "." for floats */
                return floatval(str_replace(",", ".", strval($array)));
            }

            if(is_string($array)) {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"'.str_replace($jsonReplaces[0], $jsonReplaces[1], $array).'"';
            } else {
                return $array;
            }
        }

        $isList = true;
        for($i = 0, reset($array); $i < count($array); $i++, next($array)) {
            if(key($array) !== $i) {
                $isList = false;
                break;
            }
        }

        $result = array();
        if($isList) {
            foreach ($array as $v) $result[] = json_encode2($v);
            return '['.join(',', $result).']';
        } else {
            foreach ($array as $k => $v) $result[] = json_encode2($k).':'.json_encode2($v);
            return '{'.join(',', $result).'}';
        }

    }
}






if( !function_exists('json_decode') ) {
function json_decode($data, $bool) {
if ($bool) {
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
} else {
$json = new Services_JSON();
}
return( $json->decode($data) );
}
}
?>